package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetMealMapper setMealMapper;

    @Autowired
    private SetMealDishMapper setMealDishMapper;

    @Autowired
    private DishMapper dishMapper;

    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> pageQuery = setMealMapper.pageQuery(setmealPageQueryDTO);
        Long total = pageQuery.getTotal();
        List<SetmealVO> records = pageQuery.getResult();
        return new PageResult(total, records);
    }


    @Override
    @Transactional
    public void add(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setMealMapper.add(setmeal);
        Long setmealId = setmeal.getId();
        List<SetmealDish> list = setmealDTO.getSetmealDishes();
        list.forEach(setmealDish -> setmealDish.setSetmealId(setmealId));
        setMealDishMapper.add(list);
    }


    @Override
    public SetmealVO getById(Integer setMealId) {
        SetmealVO setmealVO = new SetmealVO();
        Setmeal setmeal = setMealMapper.getById(setMealId);
        BeanUtils.copyProperties(setmeal, setmealVO);
        List<SetmealDish> list1 = setMealDishMapper.getSetMealId(setMealId);
        setmealVO.setSetmealDishes(list1);
        return setmealVO;
    }


    @Override
    public void update(SetmealDTO setmealDTO) {

        /*Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.updateDish(dish);
        dishFlavorMapper.deleteByDishId(dishDTO.getId());
        List<DishFlavor> dishFlavors = dishDTO.getFlavors();
        if (dishFlavors != null && dishFlavors.size() > 0) {
            dishFlavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishDTO.getId());
            });
            dishFlavorMapper.insertBatch(dishFlavors);
        }*/
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setMealMapper.update(setmeal);
        setMealDishMapper.deleteBySetMealId(setmealDTO.getId());
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() > 0) {
            setmealDishes.forEach(setmealDish ->
            {
                setmealDish.setSetmealId(setmealDTO.getId());
            });
            setMealDishMapper.add(setmealDishes);
        }
    }

    @Override
    public void stopOrStart(Integer status, Long id) {
        /*Setmeal setmeal = new Setmeal();
        setmeal.setId(id);
        setmeal.setStatus(status);
        setMealMapper.update(setmeal);
        if (setmeal.getStatus() == StatusConstant.DISABLE) {
            List<Long> list = new ArrayList<>();
            list.add(id);
            List<Long> setmealList = setMealMapper.getByIds(list);
            if (setmealList == null && setmealList.size() > 0) {
                for (Long setmeal1 : setmealList) {
                    Setmeal setmeal2 = new Setmeal();
                    BeanUtils.copyProperties(setmeal1, setmeal2);
                    setmeal.setStatus(StatusConstant.DISABLE);
                    setmeal.setId(Long.valueOf(setmeal1));
                    setMealMapper.update(setmeal);
                }
            }
        }*/

        if (status == StatusConstant.ENABLE) {
            List<Dish> dishList = dishMapper.getByIds(id);
            if (dishList != null && dishList.size() > 0) {
                dishList.forEach(dish -> {
                    if (dish.getStatus() == StatusConstant.DISABLE) {
                        throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                });
            }
        }
        Setmeal setmeal = new Setmeal();
        setmeal.setId(id);
        setmeal.setStatus(status);
        setMealMapper.update(setmeal);
    }

    @Override
    @Transactional
    public void delete(List<Integer> ids) {
        ids.forEach(id -> {
                    Setmeal setmeal = setMealMapper.getById(id);
                    if (setmeal.getStatus() == StatusConstant.ENABLE) {
                        throw new SetmealEnableFailedException(MessageConstant.DISH_ON_SALE);
                    }
                }
        );
        ids.forEach(id -> {
            setMealMapper.delete(id);
            setMealDishMapper.delete(id);
        });
    }
}
